package simpledb.GUI;

import java.util.Vector;

/**
 * <p>Class <code>TreeNode<E></code> is a generic class that represents
 * a tree node where each node has information about data stored in it,
 * its parent node, and a <code>Vector<TreeNode<E>></code> of children.
 *
 * @author:  Mina Hanna
 * @version: 1.0, 04/12/09
 */

public class TreeNode<E> {
	/**
	 * The generic data that can be stored in the node.
	 */
	private E data;
	
	/**
	 * The parent of this <code>TreeNode</code>.
	 */
	private TreeNode<E> parent;
	
	/**
	 * The <code>Vector</code> of children associated with this 
	 * <code>TreeNode</code>.
	 */
	private Vector<TreeNode<E>> children;
		
	public TreeNode(E nodeData) {
		setData(nodeData);
	}
	
	public TreeNode(E nodeData, Vector<TreeNode<E>> nodeChildren) {
		this(nodeData);
		setChildren(nodeChildren);
	}
	
	public E getData() {
		return data;
	}
	
	public void setData(E nodeData) {
		data = nodeData;
	}
	
	public TreeNode<E> getParent() {
		return parent;
	}
	
	public void setParent(TreeNode<E> parentNode) {
		parent = parentNode;
		parentNode.addChild(this);
	}
	
	public Vector<TreeNode<E>> getChildren() {
		return children;
	}
	
	public void setChildren(Vector<TreeNode<E>> newChildren) {
		children = newChildren;
		for(TreeNode<E> newChild : newChildren) {
			newChild.setParent(this);
		}
	}
	
	public void addChild(TreeNode<E> child) {
		if (children == null) {
			children = new Vector<TreeNode<E>>();
		}
		children.add(child);
		child.parent = this;
	}
	
	public void removeChild(TreeNode<E> child) {
		children.remove(child);
		if (children.isEmpty()) {
			children = null;
		}
	}
	
	public TreeNode<E> getChlid(int i) {
		return children.get(i);
	}
	
	public int getChildIndex(TreeNode<E> chlid) {
		return children.indexOf(chlid);
	}
	
	public boolean hasChildren() {
		return children != null;
	}
}